package com.cjf.aop.runtime;

import android.content.Context;
import android.support.annotation.NonNull;

import com.cjf.aop.runtime.checker.IThrowableHandler;
import com.cjf.aop.runtime.checker.Interceptor;
import com.cjf.aop.runtime.logger.ILogger;
import com.cjf.aop.runtime.logger.XLogger;
import com.cjf.aop.runtime.utils.PermissionUtils.OnPermissionDeniedListener;
import com.cjf.aop.runtime.utils.Strings;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * created by cjf
 * on: 2020/3/19
 */
public class AOPManager {

    private static AOPManager sInstance;
    private Context sContext;

    /**
     * 权限申请被拒绝的监听
     */
    private OnPermissionDeniedListener sOnPermissionDeniedListener;

    /**
     * 局部权限申请被拒绝的监听
     */
    private List<OnPermissionDeniedListener> localPermissionDeniedListener = new ArrayList<>();


    /**
     * 自定义拦截切片的拦截器接口
     */
    private Interceptor sInterceptor;

    /**
     * 自定义的异常处理者接口
     */
    private IThrowableHandler sIThrowableHandler;

    /**
     * 初始化
     *
     * @param context
     */
    public static AOPManager init(Context context) {
        if (sInstance == null) {
            synchronized (AOPManager.class) {
                if (sInstance == null) {
                    sInstance = new AOPManager(context);
                }
            }
        }
        return sInstance;
    }

    public static AOPManager getInstance() {
        testInitialize();
        return sInstance;
    }

    private AOPManager(Context context) {
        this.sContext = context.getApplicationContext();
    }

    /**
     * 获取全局上下文
     *
     * @return
     */
    public static Context getContext() {
        return getInstance().sContext;
    }

    private static void testInitialize() {
        if (sInstance == null) {
            throw new ExceptionInInitializerError("请先在全局Application中调用 AOPManager.init() 初始化！");
        }
    }

    //============动态申请权限失败事件设置=============//
    public static void setOnPermissionDeniedListener(@NonNull OnPermissionDeniedListener listener) {
        getInstance().sOnPermissionDeniedListener = listener;
    }

    public OnPermissionDeniedListener getOnPermissionDeniedListener() {
        return getInstance().sOnPermissionDeniedListener;
    }

    public static void addLocalOnPermissionDeniedListener(@NonNull OnPermissionDeniedListener... onPermissionDeniedListeners) {
        getInstance().localPermissionDeniedListener.clear();
        getInstance().localPermissionDeniedListener.addAll(Arrays.asList(onPermissionDeniedListeners));
    }

    public List<OnPermissionDeniedListener> getLocalPermissionDeniedListener() {
        return getInstance().localPermissionDeniedListener;
    }

    //通知局部监听
    public static void onLocalPermissionDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied, List<String> permissionsGranted){
        for (OnPermissionDeniedListener listener:getInstance().localPermissionDeniedListener){
            listener.onDenied(permissionsDeniedForever,permissionsDenied,permissionsGranted);
        }
    }

    //============磁盘缓存的序列化接口=============//

    //============自定义拦截器设置=============//

    /**
     * 设置自定义拦截切片的拦截器接口
     *
     * @param sInterceptor 自定义拦截切片的拦截器接口
     */
    public static void setInterceptor(@NonNull Interceptor sInterceptor) {
        getInstance().sInterceptor = sInterceptor;
    }

    public Interceptor getInterceptor() {
        return getInstance().sInterceptor;
    }

    //============自定义捕获异常处理=============//

    /**
     * 设置自定义捕获异常处理
     *
     * @param sIThrowableHandler 自定义捕获异常处理
     */
    public static void setThrowableHandler(@NonNull IThrowableHandler sIThrowableHandler) {
        getInstance().sIThrowableHandler = sIThrowableHandler;
    }

    public IThrowableHandler getThrowableHandler() {
        return getInstance().sIThrowableHandler;
    }

    //============日志打印设置=============//

    /**
     * 设置是否打开调试
     *
     * @param isDebug
     */
    public static void debug(boolean isDebug) {
        XLogger.debug(isDebug);
    }

    /**
     * 设置调试模式
     *
     * @param tag
     */
    public static void debug(String tag) {
        XLogger.debug(tag);
    }

    /**
     * 设置打印日志的等级（只打印改等级以上的日志）
     *
     * @param priority
     */
    public static void setPriority(int priority) {
        XLogger.setPriority(priority);
    }

    /**
     * 设置日志打印时参数序列化的接口方法
     *
     * @param sISerializer
     */
    public static void setISerializer(@NonNull Strings.ISerializer sISerializer) {
        XLogger.setISerializer(sISerializer);
    }

    /**
     * 设置日志记录者的接口
     *
     * @param logger
     */
    public static void setLogger(@NonNull ILogger logger) {
        XLogger.setLogger(logger);
    }

    //============缓存设置=============//
}
